package IO;

import org.junit.Test;
import java.io.*;
//打印流好数据流
public class printStream {
    @Test
    public void test(){
        //创建打印输出流和自动刷新模式(写入换行或'\n'会刷新缓冲区)
        try (PrintStream prinStream = new PrintStream(new FileOutputStream(new File("./user.txt")), true, "utf-8")) {
            //设置将输出写入文档
            System.setOut(prinStream);
            System.out.println(123);
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test1(){
        //创建数据写入流
        try (DataOutputStream data = new DataOutputStream(new FileOutputStream("./user.txt"))) {
            //将内容写入文档
            data.writeBoolean(true);
            data.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test2(){
        //创建数据读取流
        try (DataInputStream data = new DataInputStream(new FileInputStream("./user.txt"))) {
            //读取文档,需要按写入的顺序读取
            data.readBoolean();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
